package baseclasses;

import forms.AssignmentForm;
import com.devcon5.StudentPlanner.*;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;


public class Reminder {

	private int id;
	private int assignmentId;
	private int integer;
	private String units;


	public Reminder(){} //default constructor


	public Reminder(int id, int assignmentId, int integer, String units){ //called when created from Assignment class
		this.id = id;
		this.assignmentId = assignmentId;
		this.integer = integer;
		this.units = units;
	}

	public Reminder(int assignmentId, int integer, String units){ //called when edited from Assignment class
		this.assignmentId = assignmentId;
		this.integer = integer;
		this.units = units;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public int getAssignmentId() {
		return assignmentId;
	}


	public void setAssignmentId(int assignmentId) {
		this.assignmentId = assignmentId;
	}


	public int getInteger() {
		return integer;
	}


	public void setInteger(int integer) {
		this.integer = integer;
	}


	public String getUnits() {
		return units;
	}


	public void setUnits(String units) {
		this.units = units;
	}
	
	
	
	
	public void createAlarm(Context context, Calendar date, AlarmManager am){
        Intent intent = new Intent(context, AlarmReceiver.class); 
		intent.putExtra(AlarmReceiver.ALARM_ID, assignmentId);
        PendingIntent sender = PendingIntent.getBroadcast(context, assignmentId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //build an intent to fire when broadcast is received from AlarmReceiver class
        
        int field = -1;	
        if(units.equals(AssignmentForm.DAYS)){
        	field = Calendar.DATE;	//day of the month
        }
        else if(units.equals(AssignmentForm.MINUTES)){
        	field = Calendar.MINUTE; //minute of the day 0-59
        }
        else if (units.equals(AssignmentForm.HOURS)){
        	field = Calendar.HOUR_OF_DAY; //hour of the day 0-23
        }
        Calendar cal = date;
        cal.add(field, -1*integer); //calculate time left from time alarm is set to to field amount
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
        //once the broadcast is received, the alarm will sound
	}
	
	
	
	public void cancelAlarm(Context context, Calendar date, AlarmManager am){
        Intent intent = new Intent(context, AlarmReceiver.class);
		intent.putExtra(AlarmReceiver.ALARM_ID, assignmentId);
        PendingIntent sender = PendingIntent.getBroadcast(context, assignmentId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        
        int field = -1;
        if(units.equals(AssignmentForm.DAYS)){
        	field = Calendar.DATE;
        }
        else if(units.equals(AssignmentForm.MINUTES)){
        	field = Calendar.MINUTE;
        }
        else if (units.equals(AssignmentForm.HOURS)){
        	field = Calendar.HOUR_OF_DAY;
        }
        Calendar cal = date; //not used, just copied from above method
        cal.add(field, -1*integer);
        am.cancel(sender);
	}
	
	
	

}
